<template>
	<el-dialog v-model="dialog" :title="state.form.id ? '修改租户套餐' : '添加租户套餐'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="state.form" label-width="120px" :rules="state.rules" status-icon>
			<el-form-item label="套餐名称" prop="name">
				<el-input v-model="state.form.name" show-word-limit maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="子标题" prop="subTitle">
				<el-input v-model="state.form.subTitle"></el-input>
			</el-form-item>
			<el-form-item label="原价（元）" prop="originalPrice">
				<el-input-number :min="0.01" :precision="2" v-model="state.form.originalPrice"></el-input-number>
			</el-form-item>
			<el-form-item label="销售价格（元）" prop="salesPrice">
				<el-input-number :min="0.01" :precision="2" v-model="state.form.salesPrice"></el-input-number>
			</el-form-item>
			<el-form-item label="应用系统" prop="appKey">
				<el-select v-model="state.form.appKey" multiple>
					<el-option v-for="item in application_key" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<Editor v-model:get-html="state.form.description" height="500px" :disable="false"></Editor>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="state.form.status">
					<el-radio v-for="item in status" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { defineAsyncComponent, ref, reactive } from 'vue';
import { addObj, editObj, getById } from '/@/api/upms/tenantpackage';
import { ElMessage, FormInstance } from 'element-plus';
import { useDict } from '/@/utils/dict';

interface DataState {
	form: {
		id: string | undefined;
		status: string;
		name: string;
		subTitle: string;
		originalPrice: number;
		salesPrice: number;
		appKey: string;
		description: string;
	};
	rules: any;
}
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
const emit = defineEmits(['initPage']);
// 字典
const { application_key, status } = useDict('application_key', 'status');
const state = reactive<DataState>({
	form: { id: undefined, status: '', name: '', subTitle: '', originalPrice: 0, salesPrice: 0, appKey: '', description: '', appKeys: [] },
	rules: {
		name: [
			{
				required: true,
				message: '请输入套餐名称',
				trigger: 'change',
			},
		],
		originalPrice: [
			{
				required: true,
				message: '请输入原价（元）',
				trigger: 'change',
			},
		],
		salesPrice: [
			{
				required: true,
				message: '请输入销售价格（元）',
				trigger: 'change',
			},
		],
		appKey: [
			{
				required: true,
				message: '请选择应用系统',
				trigger: 'change',
			},
		],
		status: [
			{
				required: true,
				message: '请选择状态',
				trigger: 'change',
			},
		],
	},
});
const dialog = ref(false);
const loading = ref(false);
const formRef = ref();
const initForm = (row: any | undefined) => {
	dialog.value = true;
	if (row && row.id) {
		getDetail(row.id);
	}
};

const getDetail = (id: string) => {
	loading.value = true;
	getById(id).then((response: any) => {
		state.form = response.data.data;
		loading.value = false;
	});
};
/**
 * 关闭事件
 */
const handleClose = () => {
	resetForm(formRef.value);
};
/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	state.form.id = undefined;
	loading.value = false;
	dialog.value = false;
	formEl.resetFields();
};
/**
 * 提交按钮
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			loading.value = true;
			if (state.form.id) {
				//修改
				edit();
			} else {
				//新增
				add();
			}
		}
	});
};
/**
 * 新增
 */
const add = () => {
	addObj(state.form)
		.then((response: any) => {
			resetForm(formRef.value);
			ElMessage.success('新增成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 修改
 */
const edit = () => {
	editObj(state.form)
		.then((response: any) => {
			resetForm(formRef.value);
			ElMessage.success('修改成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
defineExpose({
	initForm,
});
</script>
